package com.archgeek.java;

import com.google.common.collect.Lists;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternTest {

    public static Pattern TABLE_DELTA_DATETIME_PATTERN = Pattern.compile("user_labels_vertical_snap_(\\d{8})");


    public static void main(String[] args) throws Exception {

        String s = "user_labels_vertical_snap_20231201";

        Matcher matcher = TABLE_DELTA_DATETIME_PATTERN.matcher(s);
        if (matcher.find()) {
            System.out.println("找到了...." + matcher.group(1) + ".....group()" + matcher.group());
        }

        // computedColumns();

    }

    private static void computedColumns() throws Exception {
        List<String> computedColumns = Lists.newArrayList("Fdate");

        System.out.println(computedColumns.stream().noneMatch(c -> c.equals("Fdate")));


        Pattern databasePattern = Pattern.compile("realtime_.*_db");
        Pattern tablePattern = Pattern.compile("t_user_macids_score_.");

//        List<MySqlSchema> mySqlSchemaList = new ArrayList<>();
        try (Connection conn = getConnection()) {
            DatabaseMetaData metaData = conn.getMetaData();
            try (ResultSet schemas = metaData.getCatalogs()) {
                while (schemas.next()) {
                    String databaseName = schemas.getString("TABLE_CAT");
                    System.out.println("databaseName====" +  databaseName);
                    Matcher databaseMatcher = databasePattern.matcher(databaseName);
                    if (databaseMatcher.matches()) {
                        try (ResultSet tables = metaData.getTables(databaseName, null, "%", null)) {
                            while (tables.next()) {
                                String tableName = tables.getString("TABLE_NAME");
                                Matcher tableMatcher = tablePattern.matcher(tableName);
                                if (tableMatcher.matches()) {
                                    System.out.println("======metaData:" + metaData + "databaseName:" + databaseName + ",tableName:" + tableName);
//                                    mySqlSchemaList.add(
//                                            new MySqlSchema(metaData, databaseName, tableName));
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    static Connection getConnection() throws Exception {

        Properties properties = new Properties();
        properties.put("user", "lx_bi_slave");
        properties.put("password", "xmywcAkCWahrOyhx3iNOwzkf5CtbFhVq");
//        properties.put("socksProxy", "127.0.0.1:1086");
//        properties.put("proxy_host", "127.0.0.1");
//        properties.put("proxy_port", "1086");

        return DriverManager.getConnection(
                String.format(
                        "jdbc:mysql://%s:%d/",
                        "10.10.16.209",
                        20006), properties);

//        return DriverManager.getConnection(
//                String.format(
//                        "jdbc:mysql://%s:%d/",
//                       " 10.10.16.209",
//                        20006),
//                "lx_bi_slave",
//                "xmywcAkCWahrOyhx3iNOwzkf5CtbFhVq");
    }

}
